package com.ruoyi.system.service.impl;

import java.text.SimpleDateFormat;
import java.util.*;

import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.mapper.FnryqkMapper;
import com.ruoyi.system.mapper.LvjudiMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.ZwryqkMapper;
import com.ruoyi.system.domain.Zwryqk;
import com.ruoyi.system.service.IZwryqkService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;

/**
 * zwryqkService业务层处理
 * 
 * @author ruoyi
 * @date 2021-10-28
 */
@Service
public class ZwryqkServiceImpl implements IZwryqkService 
{
    @Autowired
    private ZwryqkMapper zwryqkMapper;

    @Autowired
    private FnryqkMapper fnryqkMapper;

    @Autowired
    private LvjudiMapper ddLvjudiMapper;

    /**
     * 查询zwryqk
     * 
     * @param id zwryqkID
     * @return zwryqk
     */
    @Override
    public Zwryqk selectZwryqkById(String id)
    {
        return zwryqkMapper.selectZwryqkById(id);
    }

    /**
     *
     * 查询用户指定日期zwryqk
     *
     * @param userid userid
     * @param liningtime liningtime
     * @return List<String>
     */
    @Override
    public List<String> selectZwryqkByUserIdAndLiningtime(String userid,String liningtime)
    {
        return zwryqkMapper.selectZwryqkByUserIdAndLiningtime(userid,liningtime);
    }

    /**
     * 查询zwryqk列表
     * 
     * @param zwryqk zwryqk
     * @return zwryqk
     */
    @Override
    public List<Zwryqk> selectZwryqkList(Zwryqk zwryqk)
    {
        return zwryqkMapper.selectZwryqkList(zwryqk);
    }

    /**
     * 新增zwryqk
     * 
     * @param zwryqk zwryqk
     * @return 结果
     */
//    @Override
//    public int insertZwryqk(Zwryqk zwryqk)
//    {
//        return zwryqkMapper.insertZwryqk(zwryqk);
//    }

    @Override
    @Transactional
    public int insertZwryqk(Zwryqk zwryqk,String ppLvjudi)
    {
        int ok = 1;
        String mmUserId = zwryqk.getUserid();

        String mmZwryqkid = zwryqk.getId();

        String[] LvjudiList = ppLvjudi.split(";");
        if (LvjudiList !=null && LvjudiList.length !=0){
            for (int i = 0;i<LvjudiList.length;i++){
                Lvjudi mmLvjudi = new Lvjudi();
                mmLvjudi.setId(UUID.randomUUID().toString());
                mmLvjudi.setZwryqkid(mmZwryqkid);
                mmLvjudi.setLvjudi(LvjudiList[i]);
                mmLvjudi.setChuangjiantime(new Date());
                mmLvjudi.setStatus("1");
                int ok1 = ddLvjudiMapper.insertLvjudi(mmLvjudi);
                if (ok1!=1){
                    ok=0;
                }
            }
        }

        int ok3 = zwryqkMapper.insertZwryqk(zwryqk);
        if (ok3!=1){
            ok = 0;
        }

        if (fnryqkMapper.selectLatestTime(mmUserId)!=null&&zwryqkMapper.selectLatestTime(mmUserId)==null){
            String mmShijian1 = new SimpleDateFormat("yyyy-MM-dd").format(fnryqkMapper.selectLatestTime(mmUserId).get("shijian"));

            fnryqkMapper.updateShixiaoByUserId(mmUserId,mmShijian1);
            zwryqkMapper.updateShixiaoByUserId(mmUserId,mmShijian1);
        }

        if (zwryqkMapper.selectLatestTime(mmUserId)!=null&&fnryqkMapper.selectLatestTime(mmUserId)==null){
            String mmShijian2 = new SimpleDateFormat("yyyy-MM-dd").format(zwryqkMapper.selectLatestTime(mmUserId).get("shijian"));

            fnryqkMapper.updateShixiaoByUserId(mmUserId,mmShijian2);
            zwryqkMapper.updateShixiaoByUserId(mmUserId,mmShijian2);
        }

        if (zwryqkMapper.selectLatestTime(mmUserId)!=null&&fnryqkMapper.selectLatestTime(mmUserId)!=null){
            String mmShijian1 = new SimpleDateFormat("yyyy-MM-dd").format(fnryqkMapper.selectLatestTime(mmUserId).get("shijian"));
            String mmShijian2 = new SimpleDateFormat("yyyy-MM-dd").format(zwryqkMapper.selectLatestTime(mmUserId).get("shijian"));
            String mmChuanjian1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(fnryqkMapper.selectChuanjianTime(mmUserId,mmShijian1).get("chuangjiantime"));
            String mmChuanjian2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(zwryqkMapper.selectChuanjianTime(mmUserId,mmShijian2).get("chuangjiantime"));

            String mmShijian = "";
            String mmChuanjian = "";

            int i = mmShijian1.compareTo(mmShijian2);
            int j = mmChuanjian1.compareTo(mmChuanjian2);
            if (i==0){
                mmShijian = mmShijian1;
                if(j==0){
                    mmChuanjian = mmChuanjian1;
                }else if(j<0){
                    mmChuanjian = mmChuanjian1;
                }else if (j>0){
                    mmChuanjian = mmChuanjian2;
                }
                fnryqkMapper.updateShixiaoByUserIdAndTime(mmUserId,mmChuanjian);
                zwryqkMapper.updateShixiaoByUserIdAndTime(mmUserId,mmChuanjian);
            }else if (i<0){
                mmShijian = mmShijian2;
            }else if (i>0){
                mmShijian = mmShijian1;
            }
            fnryqkMapper.updateShixiaoByUserId(mmUserId,mmShijian);
            zwryqkMapper.updateShixiaoByUserId(mmUserId,mmShijian);
        }

        return ok;
    }

    /**
     * 修改zwryqk
     * 
     * @param zwryqk zwryqk
     * @return 结果
     */
    @Override
    public int updateZwryqk(Zwryqk zwryqk,String ppLvjudi)
    {
        int ok = 1;

        String mmZwryqkid = zwryqk.getId();

        Lvjudi mmLvjudi = new Lvjudi();
        mmLvjudi.setZwryqkid(mmZwryqkid);

        List<Lvjudi> mmLjdList = ddLvjudiMapper.selectLvjudiList(mmLvjudi);
        if (mmLjdList.size()==1){
            String mmLjdId = mmLjdList.get(0).getId();
            int ok1 = ddLvjudiMapper.deleteLvjudiById(mmLjdId);
            if (ok1!=1){
                ok = 0;
            }
        }

        mmLvjudi.setId(UUID.randomUUID().toString());
        mmLvjudi.setZwryqkid(mmZwryqkid);
        mmLvjudi.setLvjudi(ppLvjudi);
        mmLvjudi.setChuangjiantime(new Date());
        mmLvjudi.setStatus("1");
        int ok2 = ddLvjudiMapper.insertLvjudi(mmLvjudi);
        if (ok2!=1){
            ok=0;
        }


        int ok3 = zwryqkMapper.updateZwryqk(zwryqk);
        if (ok3!=1){
            ok = 0;
        }

        return ok;
    }

    /**
     * 删除zwryqk对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteZwryqkByIds(String ids)
    {
        return zwryqkMapper.deleteZwryqkByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除zwryqk信息
     * 
     * @param id zwryqkID
     * @return 结果
     */
    @Override
    public int deleteZwryqkById(String id)
    {
        return zwryqkMapper.deleteZwryqkById(id);
    }

    /**
     * 查询个人zwryqk列表
     *
     * @param zwryqk zwryqk
     * @return zwryqk
     */
    @Override
    public List<Map<String,Object>> selectCustomZwryqkList(ZwryqkSelect zwryqk)
    {
        List<Map<String,Object>> mmList = new ArrayList<>();

        List<Map<String,Object>> mmZwryqkList = zwryqkMapper.selectCustomZwryqkList(zwryqk);
        if (mmZwryqkList.size()!=0){
            for (int i = 0;i<mmZwryqkList.size();i++){
                Map<String,Object> mmMap = new HashMap<>();

                String mmZwryqkid = mmZwryqkList.get(i).get("id").toString();
                Lvjudi mmLvjudi = new Lvjudi();
                mmLvjudi.setZwryqkid(mmZwryqkid);
                List<Lvjudi> mmLvjudiList = ddLvjudiMapper.selectLvjudiList(mmLvjudi);
                String ljd = "";
                for (int j=0;j<mmLvjudiList.size();j++){
                    String mmLjd = mmLvjudiList.get(j).getLvjudi();
                    ljd += mmLjd + "\n";
                }
                mmZwryqkList.get(i).put("ljd",ljd);
            }
        }

        return mmZwryqkList;
    }

    @Override
    public List<ZwryqkCustom> ExportCustom(ZwryqkSelect zwryqk)
    {
        List<ZwryqkCustom> mmList = new ArrayList<>();

        List<Map<String,Object>> mmZwryqkList = zwryqkMapper.selectCustomZwryqkList(zwryqk);

        if (mmZwryqkList.size()!=0){
            for (int i = 0;i<mmZwryqkList.size();i++){

                String mmZwryqkid = mmZwryqkList.get(i).get("id").toString();
                Lvjudi mmLvjudi = new Lvjudi();
                mmLvjudi.setZwryqkid(mmZwryqkid);
                List<Lvjudi> mmLvjudiList = ddLvjudiMapper.selectLvjudiList(mmLvjudi);
                String ljd = "";
                for (int j=0;j<mmLvjudiList.size();j++){
                    String mmLjd = mmLvjudiList.get(j).getLvjudi();
                    ljd += mmLjd + "\n";
                }

                ZwryqkCustom mmZwryqkCustom = new ZwryqkCustom();
                mmZwryqkCustom.setName(mmZwryqkList.get(i).get("truename")==null?"":mmZwryqkList.get(i).get("truename").toString());
                mmZwryqkCustom.setKeshiname(mmZwryqkList.get(i).get("keshiname")==null?"":mmZwryqkList.get(i).get("keshiname").toString());
                mmZwryqkCustom.setLiningtime(mmZwryqkList.get(i).get("liningshijian")==null?"":mmZwryqkList.get(i).get("liningshijian").toString());
                mmZwryqkCustom.setLvjudi(ljd);
                mmZwryqkCustom.setZaiwaiyuanyin(mmZwryqkList.get(i).get("zaiwaiyuanyin")==null?"":mmZwryqkList.get(i).get("zaiwaiyuanyin").toString());
                mmZwryqkCustom.setRenyuanleibie(mmZwryqkList.get(i).get("leibiename")==null?"":mmZwryqkList.get(i).get("leibiename").toString());
                mmZwryqkCustom.setYanse(mmZwryqkList.get(i).get("yanse")==null?"":mmZwryqkList.get(i).get("yanse").toString());
                mmZwryqkCustom.setDianhua(mmZwryqkList.get(i).get("dianhua")==null?"":mmZwryqkList.get(i).get("dianhua").toString());
                mmZwryqkCustom.setBeizhu(mmZwryqkList.get(i).get("beizhu")==null?"":mmZwryqkList.get(i).get("beizhu").toString());

                mmList.add(mmZwryqkCustom);
            }
        }

        return mmList;
    }
}
